package com.qs.consumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qs.common.entity.Classes;
import com.qs.common.entity.Faqs;
import com.qs.common.service.ClassesService;
import com.qs.common.service.FaqsService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/faqs")
public class FaqsController {
    @Reference
    private FaqsService faqsService;
    @Reference
    private ClassesService classesService;

    @RequestMapping("/getAll")
    public List<Faqs> getAll(){
        return faqsService.findAll();
    }

    @RequestMapping("/getByKey")
    public List<Faqs> getByKey(String key){
        return faqsService.findBykey(key);
    }

    @RequestMapping("/getById")
    public Faqs getById(Long id){
        return faqsService.findById(id);
    }

    @RequestMapping("/checkTitle")
    public String checkTitle(String title){
       Faqs faqs =  faqsService.findByTitle(title);
       if(faqs!=null){
           return "标题已存在，请重新输入";
       }
       return "标题可用";
    }

    @RequestMapping("/add")
    public Map<String,String> add(String classId,String title,String content){
        Map<String,String> map = new HashMap<>();
        Faqs faqs = new Faqs();
        faqs.setClassId(Long.parseLong(classId));
        faqs.setContent(content);
        faqs.setTitle(title);
        try {
            Faqs temp =  faqsService.findByTitle(faqs.getTitle());
            if(temp!=null){
                map.put("code","1");
                map.put("message","标题已存在，请重新输入");
                return map;
            }
            faqsService.add(faqs);
            map.put("code","0");
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("code","2");
            map.put("message","系统忙，请稍后再试");
            return map;
        }
    }

    @RequestMapping("/getAllClasses")
    public List<Classes> getAllClasses(){
        return classesService.findAll();

    }




























}
